﻿using System;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using QSBusinessLayerCSharp;

namespace QSSilverlightCSharp
{
	public partial class BasicPage : UserControl
	{
		private readonly Person person;
		public BasicPage()
		{
			var mapper = new PersonMapper();
			person = mapper.GetItem(1);
			InitializeComponent();
			DataContext = person;
		}
		private void closeButton_Click(object sender, RoutedEventArgs e)
		{
			//Since Person.ValidateOnConstruction is false force a validate for values that have not changed
			person.PropertyValidationManager.ValidateAll();
			if (person.IsValid)
			{
				Utils.NavigateToPage<StartPage>();
			}
			else
			{
				var stringBuilder = new StringBuilder();
				foreach (var result in person.ValidatorResultsInError)
				{
					stringBuilder.AppendLine(result.ErrorMessage);
					stringBuilder.AppendLine(String.Format("\t\t\tRuleInterpretation = '{0}'", result.Rule.RuleInterpretation));
					stringBuilder.AppendLine(String.Format("\t\t\tRule = '{0}'", result.Rule.GetType()));
				}
				errorTextBox.Text = stringBuilder.ToString();
			}
		}
	}
}
